import Api from '../../utils/api.js';
import prompt from '../../utils/prompt.js';
import {
  checkRequire,
  checkNumber,
  checkNumberBigger,
  checkLater
} from '../../utils/validator.js';

const app = getApp();

// components/carForm/carForm.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    carTypeList: ['越野五座', '皮卡五座', '商务九座', '顺风车五座', '顺风车六座及以上乘员'], // 车辆类型列表

    personnels: '', // 人员名单
    carTime: '', // 用车时间
    carType: '', // 车辆类型
    endLocation: '', // 结束地点
    startLocation: '', // 起始地点
    peopleCount: '', // 用车人数

    ps_personnels: true,
    ps_carTime: true,
    ps_carType: true,
    ps_endLocation: true,
    ps_startLocation: true,
    ps_peopleCount: true
  },

  /**
   * 组件的方法列表
   */
  methods: {

    /* --===================数据接口===================-- */
    // 提交表单
    async submitCar () {
      const param = {
        'carNum': this.data.peopleCount,
        'carTime': this.data.carTime,
        'carType': this.data.carType,
        'destinationPoint': this.data.endLocation,
        'memberList': this.data.personnels,
        'startingPoint': this.data.startLocation
      };
      const res = await Api.applyCar(param, app.globalData.sessionKey);
      if (res) {
        prompt.showToast('车联预约成功', 'success', { mask: true });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    },

    /* --===================功能函数===================-- */
    // 输入检查
    checkInput () {
      let ps_peopleCount = false;
      let ps_startLocation = false;
      let ps_endLocation = false;
      let ps_carType = false;
      let ps_carTime = false;
      let ps_personnels = false;

      ps_peopleCount = (
        checkRequire(this.data.peopleCount, '请输入用车人数') &&
        checkNumber(this.data.peopleCount, '请输入正确的用车人数') &&
        checkNumberBigger(this.data.peopleCount, 1, '用车人数应大于1人')
      );
      this.setData({ ps_peopleCount });

      if (ps_peopleCount) {
        ps_startLocation = checkRequire(this.data.startLocation, '请选择起始地点');
        this.setData({ ps_startLocation });
      }

      if (ps_startLocation) {
        ps_endLocation = checkRequire(this.data.endLocation, '请选择目的地');
        this.setData({ ps_endLocation });
      }

      if (ps_endLocation) {
        ps_carType = checkRequire(this.data.carType, '请选择车辆类型');
        this.setData({ ps_carType });
      }

      if (ps_carType) {
        const current = new Date();
        ps_carTime = (
          checkRequire(this.data.carTime, '请选择用车时间') &&
          checkLater(new Date(this.data.carTime), current, '无效的用车时间')
        );
        this.setData({ ps_carTime });
      }

      if (ps_carTime) {
        ps_personnels = checkRequire(this.data.personnels, '请输入用车人员名单');
        this.setData({ ps_personnels });
      }

      return (
        ps_peopleCount &&
        ps_startLocation &&
        ps_endLocation &&
        ps_carType &&
        ps_carTime &&
        ps_personnels
      );
    },

    /* --===================事件处理===================-- */
    // 数输入事件
    handleInput (e) {
      const param = {};
      param[e.currentTarget.dataset.state] = e.detail.value;
      this.setData(param);
    },
    // 获取 picker input 组件值
    handlePickerInputValue (e) {
      const param = {};
      param[e.currentTarget.dataset.value] = e.detail.value;
      this.setData(param);
    },
    // 点击选择起始地点
    handleTapStartLocation (e) {
      wx.chooseLocation({
        success: (result) => {
          if (result) {
            const param = {};
            param[e.currentTarget.dataset.state] = result.name;
            this.setData(param);
          }
        }
      });
    },
    // 表单提交
    handleSubmit () {
      if (this.checkInput()) {
        this.submitCar();
      }
    }
  }
});
